package com.jinlejia.erp.ui.home;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.VisitScheduleAdapter;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivityVisitPreviewBinding;
import com.jinlejia.erp.utils.DataFilter;
import com.jinlejia.erp.utils.DateUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import okhttp3.Call;

/**
 * 预览（activity）
 */

public class PreviewActivity extends AppCompatActivity {
    private VisitScheduleAdapter adapter;
    private ArrayList<Map<String, Object>> dataList = new ArrayList<>();

    private ActivityVisitPreviewBinding binding;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_visit_preview);
        binding.backBtn.setOnClickListener(v->finish());
        EventBus.getDefault().registerSticky(this);
        initView();
    }

    @Override
    public void onStart() {
        super.onStart();
        getData();
    }

    /**
     * 初始化视图
     */
    private void initView() {

        if (getIntent().getStringExtra("state").equals("0")) {//修改
            binding.time.setText(DateUtil.getTime(getIntent().getStringExtra("firsttime"), "yyyy-MM-dd").concat(" " + DateUtil.getTime(getIntent().getStringExtra("firsttime"), "HH:mm") + " - " + DateUtil.getTime(getIntent().getStringExtra("endtime"), "HH:mm")));
            binding.houseAmount.setText("约看房源（" + getIntent().getIntExtra("number", 0) + "）");
        } else { //添加
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
            try {
                Date firsttime = sdf1.parse(getIntent().getStringExtra("firsttime").concat(":00:00"));
                Date endtime = sdf1.parse(getIntent().getStringExtra("endtime").concat(":00:00"));
                binding.time.setText(DateUtil.getTime(firsttime.getTime()) + " " + DateUtil.getTime(firsttime.getTime(), "HH:mm") + " - " + DateUtil.getTime(endtime.getTime(), "HH:mm"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            binding.houseAmount.setText("约看房源（" + getIntent().getIntExtra("number", 0) + "）");

        }
        binding.previewRecycleView.setHasFixedSize(true);
        binding.previewRecycleView.setLayoutManager(new LinearLayoutManager(this));

        adapter = new VisitScheduleAdapter(dataList, 3);
        binding.previewRecycleView.setAdapter(adapter);
    }


    @Subscriber(tag = "TAG_PREVIEW")
    private void getPreview(ArrayList<Map<String, Object>> data) {
        EventBus.getDefault().removeStickyEvent(data.getClass(), "TAG_PREVIEW");
        if (data.size() > 0) {
            this.dataList = data;
            adapter.setData(data);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 获取数据
     */
    public void getData() {
        OkHttpUtils
                .get()
                .url(ApiConfig.ACQUIRE_USER_INFO)
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(PreviewActivity.this, response, null)) {
                            Map<String, Object> map = JSON.parseObject(response.get("data").toString(), new TypeReference<Map<String, Object>>() {
                            });
                            String LoginName = "";
                            if( map.containsKey("name") ) {
                                 LoginName = map.get("name").toString();
                            }else if( map.containsKey("loginName") ){
                                 LoginName = map.get("loginName").toString();
                            }
                            binding.location.setText("在" +getIntent().getStringExtra("place")+"由"+ LoginName + "带看" + "" + getIntent().getIntExtra("number", 0) + "套房");
                        }
                    }
                });
    }

}
